package com.auction.controller.user;

import com.auction.dto.LoginDTO;
import com.auction.dto.UserDTO;
import com.auction.pojo.Result;
import com.auction.pojo.User;
import com.auction.service.UserService;
import com.auction.utils.BaseContext;
import com.auction.vo.UserLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * 
     * @param loginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody LoginDTO loginDTO) {
        log.info("用户登录：{}", loginDTO);
        UserLoginVO loginVO = userService.UserLogin(loginDTO);
        return Result.success(loginVO);
    }

    /**
     * 用户注册
     * 
     * @param userDTO
     * @return
     */
    @PostMapping("/register")
    public Result<String> register(@RequestBody UserDTO userDTO) {
        log.info("用户注册：{}", userDTO);
        userService.register(userDTO);
        return Result.success("注册成功");
    }

    /**
     * 获取当前登录用户信息
     * 
     * @return
     */
    @GetMapping("/info")
    public Result<User> info() {
        Long userId = BaseContext.getCurrentId();
        User user = userService.getById(userId);
        return Result.success(user);
    }

    /**
     * 更新用户信息
     * 
     * @param userDTO
     * @return
     */
    @PutMapping
    public Result<String> update(@RequestBody UserDTO userDTO) {
        log.info("更新用户信息：{}", userDTO);
        userService.update(userDTO);
        return Result.success("修改成功");
    }
}